<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Custom payloads - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../tutorial/advanced.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../tutorial/advanced.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/advanced/payload/copy_file.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">

  <div class="titlepage"><div><div><h1 style="clear: both">Custom payloads</h1></div></div></div>
<p>So far in this tutorial, type <code>EC</code> in <code>result&lt;T, EC&gt;</code> has always been a
<code>std::error_code</code> or <code>boost::system::error_code</code>. With <code>NoValuePolicy</code>
set to <a href="../../reference/aliases/default_policy.html" class="api-reference"><code>default_policy&lt;T, EC, EP&gt;</code></a>
, <code>EC</code> needs
in fact to merely satisfy the trait
<a href="../../reference/traits/is_error_code_available.html" class="api-reference"><code>is_error_code_available&lt;T&gt;</code></a>

for <code>EC</code> to be treated as if an <code>error_code</code>. Outcome specialises that
trait for <code>std::error_code</code> and <code>boost::system::error_code</code>,
hence they &ldquo;just work&rdquo;.</p>

<p>If no specialisation exists, <code>trait::is_error_code_available&lt;EC&gt;</code> is true
if there exists some ADL discovered free function <code>make_error_code(EC)</code>.</p>

<p>Thus, we can in fact use any custom <code>EC</code> type we like, including one carrying additional
information, or <em>payload</em>. This payload can carry anything you like, and you can tell
Outcome to do various things with that payload under various circumstances. For example:</p>

<ol>
<li>If the user tries to observe an unsuccessful <code>result</code>, throw a custom exception
containing the cause of failure with accompanying context from the payload.</li>
<li>If the user ever constructs an <code>outcome</code> from a payload carrying <code>result</code>,
set the exception ptr in the constructed <code>outcome</code> to a custom exception
containing the cause of the failure with accompanying context from the payload.</li>
<li>Transmit a stack backtrace specifying the exact point at which failure occurred,
symbolising that backtrace into human readable text at the moment of conversion into human
readable text.</li>
<li>Upon a namespace-localised <code>result</code> from library A being copy/moved into a
namespace-localised <code>result</code> from C bindings library B, set the C <code>errno</code> if
the error code and category map onto the <code>errno</code> domain.</li>
</ol>

<p>There are many, many other options of course. This tutorial can only cover a
reasonable subset. This section covers Example 1 above, throwing custom exceptions
with payload upon observation of an unsuccessful <code>result</code>.</p>



        </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../tutorial/advanced.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../tutorial/advanced.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/advanced/payload/copy_file.html"><img src="../../images/next.png" alt="Next"></a></div></body>
</html>
